package voidgame.core;
import java.util.ArrayList;
import java.util.Random;

public class Galaxy{
	
	Game game;
	
	ArrayList<StarSystem> systems;
	
	ArrayList<Species> species;
	
	String[] astralnamelist;
	int anlcount;
	
	String[] lifeformnamelist;
	int lflcount;
	
	String name;
	
	public Galaxy(Game g, int size){
		
		game = g;
		systems = new ArrayList<StarSystem>();
		species = new ArrayList<Species>();
		astralnamelist = game.ng.astralList(5000);
		anlcount = 0;
		lifeformnamelist = game.ng.lifeformNameList(100);
		lflcount = 0;
		
		name = getAstralName();
		
		for(int x = 0; x<size; x++){
			systems.add(new StarSystem(this));			
		}
		
		StarSystem solSystem = new StarSystem(this);
		solSystem.makeSol();
		systems.add(solSystem);	
		
		genWormHoles();
		
		genLifeForms();
		
		
		
	}
	
	public String getAstralName(){
		String n = astralnamelist[anlcount];
		
		anlcount++;
		if(anlcount>=astralnamelist.length){
			System.err.println("ANL Overflow, regenerating");
			astralnamelist = game.ng.astralList(5000);
			anlcount=0;
		}
		
		return n;
	}
	
	public String getLifeFormName(){
		String n = lifeformnamelist[lflcount];
		
		lflcount++;
		if(lflcount>=lifeformnamelist.length){
			System.err.println("LFL Overflow, regenerating");
			lifeformnamelist = game.ng.astralList(100);
			lflcount=0;
		}
		
		return n;
	}
	
	
	public void genWormHoles(){
		systems.get(systems.size()-1).addWormHole();		
	}
	
	public void genLifeForms(){
		Random ran = new Random();
		for(StarSystem sys:systems){
			for(Planet p:sys.planets){
				if(p.life){
					if(ran.nextInt(3)==0){
						Species intSpecies = new Species(p,getLifeFormName(),true);
						species.add(intSpecies);
					}
				}
				
				int noSpecies = ran.nextInt(3)+5;
				
				for(int i = 0;i<noSpecies;i++){
					Species genSpecies = new Species(p,game.ng.lifeformName(p),false);
					species.add(genSpecies);
				}
				
			}
		}
	}
	
	public void printQuickDesc(){
		
		System.out.println("Galaxy: "+this.name);
		for(StarSystem s: this.systems){
			System.out.println(s.name+" system");
			String holesDesc="";
			for(Wormhole w:s.wormholes){
				holesDesc+="hole("+w.alpha.name+","+w.beta.name+"), ";
			}
			System.out.println("   has "+s.wormholes.size()+" wormholes. "+holesDesc);
			System.out.println("   star: "+s.star.name+": "+s.star.desc);
			for(Planet p: s.planets){
				System.out.println("      "+p.name+": "+p.desc);
				String moonString = "         moons: ";
				for(Moon m: p.moons){
					moonString+=m.name+" "+m.desc+", ";
				}
				System.out.println(moonString);
			}
		}
		
	}
	
	
	
}